home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / vs / browser / cpb10eb.exe / DATA.Z / clock.java < prev    next >
Text File  |  1996-09-25  |  5KB  |  188 lines

  1. // clock.java
  2. // 
  3. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  4. //
  5. import vrml.*;
  6. import vrml.field.*;
  7. import vrml.node.*;
  8. import java.util.*;
  9.  
  10. public class clock extends Script{
  11.  
  12.     SFRotation setMin  ;
  13.     SFRotation setHour ;
  14.     SFRotation rotation;
  15.  
  16.     float aRad = (float)(Math.PI/180.0);
  17.  
  18.     float clockRotation[] = {0.0f,1.0f,0.0f,0.0f} ;
  19.     float rotDgr = 0.0f ;
  20.     boolean rotating = false ;
  21.  
  22.     /* second parts*/
  23.     int secondsMax = 60;
  24.     SFColor seconds[] = new SFColor[secondsMax];
  25.     String secondsName[] = 
  26.       {"sec0","sec1","sec2","sec3","sec4","sec5", 
  27.      "sec6","sec7","sec8","sec9","sec10","sec11", 
  28.      "sec12","sec13","sec14","sec15","sec16","sec17", 
  29.      "sec18","sec19","sec20","sec21","sec22","sec23", 
  30.      "sec24","sec25","sec26","sec27","sec28","sec29", 
  31.      "sec30","sec31","sec32","sec33","sec34","sec35", 
  32.      "sec36","sec37","sec38","sec39","sec40","sec41", 
  33.      "sec42","sec43","sec44","sec45","sec46","sec47", 
  34.      "sec48","sec49","sec50","sec51","sec52","sec53", 
  35.      "sec54","sec55","sec56","sec57","sec58","sec59"} ;
  36.     float secColors[][] ;
  37.  
  38.     /* quarter min parts */
  39.     SFColor Ss[]=new SFColor[4];
  40.     String sName[] = { "s0","s15","s30","s45" };
  41.     float sColors[][] ;
  42.  
  43.     boolean initialized = false ;
  44.     // rotation parameters.
  45.     float rotate[];
  46.     public int min, hour, sec;
  47.        
  48.     boolean doReset = true ; 
  49.  
  50.     public void initialize(){
  51.     setMin  = (SFRotation) getEventOut("setMin");
  52.     setHour = (SFRotation) getEventOut("setHour");
  53.     rotation = (SFRotation) getEventOut("rotation");
  54.  
  55.     MFColor c = (MFColor)getField("secColors");
  56.     int     s = c.getSize();
  57.     secColors = new float[s][3];
  58.     c.getValue(secColors);
  59.  
  60.     c = (MFColor)getField("sColors");
  61.     s = c.getSize();
  62.     sColors = new float[s][3] ;
  63.     c.getValue(sColors);
  64.     
  65.     int i ;
  66.     // get time
  67.     Date currentDate = new Date();
  68.     
  69.     /* get second panels */
  70.     for(i=0;i<secondsMax;i++){
  71.         seconds[i] = (SFColor) getEventOut(secondsName[i]);
  72.     }
  73.     for(i=0;i<4;i++){
  74.         Ss[i] = (SFColor) getEventOut(sName[i]);
  75.     }
  76.  
  77.         // rotation axle's direction.
  78.         rotate = new float[4];
  79.         rotate[0] = 0.0f;
  80.         rotate[1] = 0.0f;
  81.         rotate[2] = 1.0f;
  82.  
  83.         sec = currentDate.getSeconds();
  84.         min = currentDate.getMinutes();
  85.         hour = currentDate.getHours();
  86.         System.out.println( "min,hour:" + min + " " + hour);
  87.  
  88.     resetSec();    
  89.         // rotation axle's position.
  90.     setMin( min );
  91.     setHour( hour, min );
  92.  
  93.     }
  94.     public void processEvent(Event e){
  95.     String name = e.getName();
  96.     
  97.     if(name.equals("tick")){
  98.         tick((ConstSFTime)e.getValue(), e.getTimeStamp());
  99.     }
  100.     if(name.equals("clicked")){
  101.         clicked ( (ConstSFBool)e.getValue(), e.getTimeStamp());
  102.     }
  103.     }
  104.  
  105.     public void tick(ConstSFTime arg, double ts){
  106.     // get time
  107.     Date currentDate = new Date();
  108.  
  109.         sec = currentDate.getSeconds();
  110.         min = currentDate.getMinutes();
  111.         hour = currentDate.getHours();
  112.  
  113.     if ( sec == 0 || ! initialized ) {
  114.         // rotation axle's position.
  115.         setMin( min );
  116.         setHour( hour, min );
  117.         initialized = true ;
  118.  
  119.     }
  120.     setSec ( sec ) ;
  121.         if(rotating) {
  122.             rotDgr += 10.0f;
  123.             if (rotDgr > 360.0f) {
  124.                 rotDgr -= 360.0f;
  125.             }
  126.             clockRotation[3] = (aRad * rotDgr) ;
  127.             rotation.setValue(clockRotation);
  128.         }
  129.     }
  130.     void setMin ( int min ){
  131.         rotate[3] = (-6.0f * min)*aRad;
  132.         setMin.setValue(rotate);
  133.  
  134.     }
  135.     void setHour ( int hour, int min ) {
  136.         rotate[3] = ((-30.0f * hour) +(-30.0f *(min/60.0f))) * aRad;
  137.         setHour.setValue(rotate);
  138.     }
  139.     int last = 0 ;
  140.     void setSec(int sec){
  141.     if(last > sec){ 
  142.         resetSec();
  143.     }
  144.     for(int i=last;i<=sec;i++){
  145.         secColors[i][0]=1.0f;
  146.             seconds[i].setValue(secColors[i]);
  147.         if(i==0||i==15||i==30||i==45){
  148.                 int j = i/15;
  149.         sColors[j][0]=1.0f;
  150.         Ss[j].setValue(sColors[j]);
  151.         }
  152.     }
  153.         last = sec ;
  154.     }
  155.     void resetSec(){
  156.         int i ;
  157.     for(i=0;i<secondsMax;i++){
  158.         secColors[i][0]=0.0f;
  159.         secColors[i][2]=0.5f;
  160.         seconds[i].setValue(secColors[i]);
  161.     }
  162.     for(i=0;i<4;i++){
  163.         sColors[i][0]=0.0f;
  164.         sColors[i][2]=0.5f;
  165.         Ss[i].setValue(sColors[i]);
  166.     }
  167.         last = 0 ;
  168.     }
  169.  
  170.     public void clicked ( ConstSFBool arg, double now ) {
  171.         if ( arg.getValue() ) return ;
  172.         if (rotating) {
  173.             rotDgr = 0.0f;
  174.             clockRotation[3] = 0.0f;
  175.             rotation.setValue(clockRotation);
  176.             rotating = false ;
  177.         }         
  178.         else {
  179.             rotating = true ;
  180.         }
  181.         String s[] = new String[1] ;
  182.         s[0] = "./html/clock.html";
  183.         getBrowser().loadURL ( s , null ) ;
  184.     }
  185. }
  186.  
  187.  
  188.